//
//  NetTools.m
//  OrderAssistant
//
//  Created by chendong on 2020/8/28.
//  Copyright © 2020 chendong. All rights reserved.
//

#import "NetTools.h"
#import <AFNetworking.h>
static AFHTTPSessionManager *_manager;
@interface NetTools()
@end
@implementation NetTools
+ (void)initialize{
    [super initialize];
    _manager = [AFHTTPSessionManager manager];
}
+ (instancetype)sharedSingleton {
    static NetTools *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //不能再使用alloc方法
        //因为已经重写了allocWithZone方法，所以这里要调用父类的分配空间的方法
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}
// 防止外部调用alloc 或者 new
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [NetTools sharedSingleton];
}
// 防止外部调用copy
- (id)copyWithZone:(nullable NSZone *)zone {
    return [NetTools sharedSingleton];
}
// 防止外部调用mutableCopy
- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [NetTools sharedSingleton];
}
+(void)getWithUrl:(NSString *)url pararms:(NSDictionary *)dic success:(void (^)(id json))success fail:(void (^)(NSError *error))fail{
    //申明请求的数据是json类型
    _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //申明返回的结果是json类型
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    _manager.requestSerializer.timeoutInterval = 10.0f;
    //如果报接受类型不一致请替换一致text/html或别的
    _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript",@"text/plain", nil];
    [_manager GET:url parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
//        NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8);
        NSString *receiveStr = [[NSString alloc]initWithData:responseObject encoding:encoding];
        NSData * aData = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        id response;
        if (aData == nil) {
            NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8);
            NSString *receiveStr = [[NSString alloc]initWithData:responseObject encoding:encoding];
            NSData * aData = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error = nil;
            if (aData == nil) {
                
            }else{
                response  = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableContainers error:&error];
            }
        }else{
          response  = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableContainers error:&error];
        }
        success(response);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
    }];
}
+(void)postWithUrl:(NSString *)url pararms:(NSDictionary *)dic success:(void (^)(id json))success fail:(void (^)(NSError *error))fail{
    //申明请求的数据是json类型
    _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //申明返回的结果是json类型
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript",@"text/plain",@"text/plain; charset=utf-8",nil];
    
    [_manager POST:url parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        NSString *receiveStr = [[NSString alloc]initWithData:responseObject encoding:encoding];
        NSData * aData = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        id response = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableContainers error:&error];
        success(response);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
    }];
}
+(void)utfPostWithUrl:(NSString *)url pararms:(NSDictionary *)dic success:(void (^)(id json))success fail:(void (^)(NSError *error))fail{
    //申明请求的数据是json类型
    _manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //申明返回的结果是json类型
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript",@"text/plain",@"text/plain; charset=utf-8",nil];
    // 设置头部
    [_manager.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    
    [_manager POST:url parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        NSString *receiveStr = [[NSString alloc]initWithData:responseObject encoding:encoding];
        NSData * aData = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        id response = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableContainers error:&error];
        success(response);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
    }];
}
@end
